public class Sort {
    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
        System.out.println(BinarySearch(nums, 100));
        System.out.println(BinarySearch(nums, 15));
    }
    //二分查找
    private static int BinarySearch(int[] nums, int targer) {
        int left = 0;
        int right = nums.length-1;
        int mid = (left+right)/2;
        while(left <= right) {
            if(nums[mid] < targer) {
                left = mid+1;
                mid = left + (right-left)/2;
            }
            else if(nums[mid] > targer) {
                right = mid -1;
                mid = (left+right)/2;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
}
